﻿
import Request from './request.js'
import Storage from './storage.js'
const Composition = (proxy) => {
    const data = reactive({
        messageTest: "",
        proxy: "",
        test: "11",
        loading: true,
        state: {
            collapsed: false,
            selectedKeys: [''],
            openKeys: [''],
            //preOpenKeys: ['sub1'],
        },
        items: [

        ]
    })
    let params = toRefs(data)







    const methods = {
        tClick: (e) => {
            try { methods.getAllKey(e.key) }
            catch (ee) {
                if (ee == 'forceBreakOp') {
                    //终止循环罢了
                }

            }

            console.log(e, 'eee')
            let Allm = Storage.get('LeftMenu', true)||[]
            let a = e.keyPath[0]

            if (!Array.prototype.findIndex) {
                Array.prototype.findIndex = function (callback, thisArg) {
                    var thisObj = this;
                    var len = this.length >>> 0;

                    for (var i = 0; i < len; i++) {
                        if (i in thisObj && callback.call(thisArg, thisObj[i], i, thisObj)) {
                            return i;
                        }
                    }
                    return -1;
                };
            }
            let index = Allm.findIndex(es => es.PartName == a.split('-')[1])
            if (index != -1) {
                window.localStorage.setItem('mindex', index)
            }
  
            window.location.href = e.item.originItemValue.key
            window.localStorage.setItem('menue', JSON.stringify(e))

       
  

        },
        cClick: () => {
  
            //params.state.value.collapsed = !params.state.value.collapsed

            //document.querySelector('.static-sidebar-wrapper').style.width = '0px'
            //document.getElementById("toggle_sidemenu_l").click();

        },
        getAllKey: (select = '..' + window.location.pathname) => {


            let l1 = ""
            let l2 = ""

            params.items.value.forEach((e, i) => {
                l1 = e.key
                if (e.children && e.children.length > 0) {

                    e.children.forEach((es, is) => {
                        l2 = es.key
                        if (es.children && es.children.length > 0) {
                            //es.icon = h('img', { width: '24px', height: '24px', src: 'https://aliyuncdn.antdv.com/form/static/logo-blue.png' })
                            es.children.forEach((ess, iss) => {
                                //ess.icon = h('img', { width: '24px', height: '24px', src: 'https://aliyuncdn.antdv.com/form/static/logo-blue.png' })
                                if (select == ess.key) {

                                    params.state.value.openKeys = [l1, l2, select]
                                    params.state.value.selectedKeys = [select]

                                    throw Error('forceBreakOp');         //满足条件，跳出循环
                                } else {

                                    let mm = window.localStorage.getItem('menue') ? JSON.parse(window.localStorage.getItem('menue')).keyPath : []
                                    let cc = window.localStorage.getItem('menue') ? JSON.parse(window.localStorage.getItem('menue')).key : []

                                    params.state.value.openKeys = mm
                                    params.state.value.selectedKeys = [cc]
                                }
                            })

                        } else {

                        }
                    })
                } else {

                }

            })

       
        },


        init: () => {

 
            params.loading.value = true
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 2,
                maxCount: 1,
                rtl: true,
            });
            setTimeout(() => {
                //let info = Storage.get('AccountInfo', true)
                //Request({
                //    url: "/Account/createJwtToken",
                //    method: 'post',
                //    data: {
                //        "userName": info && info.UserNo
                //    }
                //}).then(res => {
                //    Storage.set('token', res.Data)

                let isMenue = Storage.get('LeftMenu',true)

                if (isMenue) {
                    let ff = isMenue.map((e, i) => {
                      
                        let o1 = {}
                        o1.key = 'a' + i + '-' + e.PartName
                        o1.label = e.PartName
                        o1.title = ""


                        o1.icon = h('span', { width: '24px', height: '24px', class: e.MenuList[0].Icon })
                        o1.children = e.MenuList.map((es, ei) => {
                            let o2 = {}
                            o2.key = 'b' + ei + es.MenuName
                            o2.label = es.MenuName
                            o2.icon = h('span', { width: '24px', height: '24px', class: es.Icon })
                            o2.title = ""
                            o2.children = es.NodeList.map((ess, iss) => {
                                let o3 = {}
                                o3.key = ess.Path
                                o3.label = ess.NodeName
                                o3.title = ""
                                o3.icon = h('span', { width: '24px', height: '24px', class: ess.Icon })
                                return o3
                            })
                            return o2
                        })
                        return o1
                    })

                    params.items.value = ff
       
                    params.loading.value = false

                } else {
                    Request({
                        url: "/Account/GetLeftMenu",
                        method: 'post',
                    }).then(res => {

         
       

                        let ff = res.Data.map((e, i) => {
                            let o1 = {}
                            o1.key = 'a' + i +'-'+ e.PartName
                            o1.label = e.PartName
                            o1.title = ""


                            o1.icon = h('span', { width: '24px', height: '24px', class: e.MenuList[0].Icon })
                            o1.children = e.MenuList.map((es, ei) => {
                                let o2 = {}
                                o2.key = 'b' + ei + es.MenuName
                                o2.label = es.MenuName
                                o2.icon = h('span', { width: '24px', height: '24px', class: es.Icon })
                                o2.title = ""
                                o2.children = es.NodeList.map((ess, iss) => {
                                    let o3 = {}
                                    o3.key = ess.Path
                                    o3.label = ess.NodeName
                                    o3.title = ""
                                    o3.icon = h('span', { width: '24px', height: '24px', class: ess.Icon })
                                    return o3
                                })
                                return o2
                            })
                            return o1
                        })

                        params.items.value = ff
                        params.loading.value = false
       

                    })
                }


                setTimeout(() => {
                    try {
                        methods.getAllKey()

                    }
                    catch (ee) {
                        if (ee == 'forceBreakOp') {
                            //终止循环罢了
                        }

                    }
                }, 100)


            }, 400)




            setTimeout(() => {
                let s = document.querySelector('#LayoutApp')
                let index = Number(Storage.get('mindex'))
                s.scrollTop = 100*index
            },2000)

        },





    }

    const template = {
        bottomBtns: {
            template: `<div class="w-full bg-[#eeeeee] h-50  flex  items-center justify-end">
                    <a-space>
                        <a-button v-on:click="onBack">返回</a-button>
                    </a-space>
                </div>`,
            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition
